<?php

		
		class activeAction extends userAction {
			
			private $feedSetting = false; //操作是否加入动态
			protected function _initialize() {
				parent::_initialize();
				$this->feedSetting = getAddFeedSetting($this->userInfo->id,"activeaction"); 
			}
			
			/**
			 * 活动列表
			 */
			public function index(){
				isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u = "all";
				import("ORG.Util.Page");
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;
				$dao = D("active"); 
				if("all" == $u){//所有活动
					import("ORG.Util.Page");
					$activeCount = $dao->count("privacy>0","id");
					$listRows 	= 	C("publicListRows");
					$p 			= 	new Page($activeCount,$listRows);
					$p			->	setConfig('header' ,'个活动 '); 
					$activeList	=	$dao->findAll("privacy>0","id,activename,activetype,startime,endtime,activename",'starttime desc',$p->firstRow.','.$p->listRows);//
					$page  		= 	$p->show();
					$this->assign("activelist",$activeList);
					$this->assign("page",$page);
					$this->display("all");
				}else{//某个人的活动
					$activeids = D("active_user")->findAll("userid=".$u,"activeid");
					$activeid = array(); //构造friend的ID数组
					foreach ($activeids as $item){
						$activeid[] = $item->activeid;
					}
					$ids= implode(",",$activeid);
					$activeCount = $dao->count("id in ($ids) and privacy>0","*");
					$listRows 	= 	C("publicListRows");
					$p 			= 	new Page($activeCount,$listRows);
					$p			->	setConfig('header' ,'个活动 '); 
					$activeList	=	$dao->findAll("id in ($ids)","id,activename,activetype,startime,endtime,activename",'starttime asc',$p->firstRow.','.$p->listRows);
					$page  		= 	$p->show();
					if($u != $this->userInfo->id){
						$userInfo = D("user")->find("id=".$u,"id,username");
						$this->assign("userInfo",$userInfo);
					}
					$this->assign("myId",$this->userInfo->id);
					$this->assign("activelist",$activeList);
					$this->assign("page",$page);
					$this->display();
				}
			}
			
			/**
			 * 好友参加的活动
			 */
			public function friendactive(){
				$friends = getFriendList($this->userInfo->id);
				$activeids = D("active_user")->findAll("userid in ($friends)","activeid");
				$activeid = array(); //构造friend的ID数组
				foreach ($activeids as $item){
					$activeid[] = $item->activeid;
				}
				$ids= implode(",",$activeid);
				$activeCount = $dao->count("id in ($ids) and privacy>0","*");
				$listRows 	= 	C("publicListRows");
				$p 			= 	new Page($activeCount,$listRows);
				$p			->	setConfig('header' ,'个活动 '); 
				$activeList	=	$dao->findAll("id in ($ids) and privacy>0","id,activename,activetype,startime,endtime,activename",'starttime desc',$p->firstRow.','.$p->listRows);
				$page  		= 	$p->show();
				$this->assign("activelist",$activeList);
				$this->assign("page",$page);
				$this->display();
			}
			
			/**
			 * 发起一个活动
			 */
			public function createactive(){
				$this->display();
			}
			/**
			 * 保存入库
			 */
			public function saveactive(){
				$dao = new activeModel();
				if($dao->create()){
					$dao->userid = $this->userInfo->id;
					$id = $dao->add();
					if($id>0){
						$this->redirect("editactiveface","active","",APP_NAME,array("id"=>$id));//跳转到活动头像设置
					}else{
						$this->error("创建失败，你可以重试一次");
					}
				}else{
					$this->error($dao->getError());
				}
			}
			/**
			 * 设置活动头像
			 */
			public function editactiveface(){
				$id = safeRequest($_GET["id"]);
				$dao = new activeModel();
				$record = $dao->find("id=".$id);
				if($record){
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
					$this->assign("record",$record);
					$this->display();
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
				}
				
			}
			
			//保存活动头像
			public function saveactiveface(){
				import("ORG.Net.UploadFile");
	        	$upload = new UploadFile();
	        	$upload->allowExts = array("jpg","gif","png","bmp");
	        	$upload->savePath = "./Public/Uploads/active/";
	        	$upload->fileExtension = true;
	        	$upload->saveRule = "uniqid";
				if($upload->upload()){
					$img = $upload->getUploadFileInfo();
					$activeid = safeRequest($_POST["id"]);
					$dao = new activeModel();
					$dao->find("id=".$activeid,"activephoto");
					$oldimg = $dao->activephoto;
					$dao->activephoto = $img[0]["savename"];
					$dao->save();
					if(isset($_POST["edit"])){//编辑状态
						@unlink($upload->savePath.$oldimg);
						$this->success("修改头像成功");
					}else{
						$this->redirect('setactiveuser','active','',APP_NAME,array('id'=>$activeid));
					}
				}else{
					$this->error($upload->error);
				}
			}
			
			/**
			 * 成员管理
			 */
			public function setactiveuser(){
			}
			
			/**
			 * 编辑活动
			 */
			public function editactive(){
				$id= safeRequest($_GET["id"]);
				$record = D("active")->find("id=".$id);
				if($record){
					$this->assign("record",$record);
					$this->display();
				}else{
					$this->error("错误的操作");
				}
			}
			/**
			 * 保存编辑
			 */
			public function updateactive(){
				$dao = new activeModel();
				if($dao->create()){
					if($dao->save()){
						$this->success("编辑成功");
					}else{
						$this->error("编辑失败，你可以重试一次");
					}
				}else{
					$this->error($dao->getError());
				}
			}
			
			/**
			 *显示活动
			 */
			public function showactive(){
				$id = safeRequest($_GET["id"]);
				$dao = new activeModel();
				$record = $dao->find("id=".$id);
				if($record){
					$privacy = $record->privacy; //活动的设置权限
					$manage  = 0; //我的浏览权限
					$userdao = new active_userModel();
					if($record->userid == $this->userInfo->id){
						$ismanage = 3; //我是活动发起人
					}else{
						$userrecord = $userdao->find("activeid=".$id." and userid=".$this->userInfo->id,"usertype");
						if($userrecord){
							$userrecord->usertype == 1 ? $manage=1 : $manage=2; 
						}
					}
					if($privacy==0 && $manage=0){//私密活动，仅成员能浏览
						$this->error("由于隐私设置,不能浏览");
					}else{
						$userlist = $userdao->findAll("activeid=".$id,"userid","",6);//显示6个成员
						$guestlist = D("active_guest")->findAll("activeid=",$id,"*","regtime desc",15);//最新15条留言
						$photolist = D("active_photo")->findAll("activeid=".$id,"*","regtime desc",6);//最新活动相片
						$this->assign("record",$record);
						$this->assign("userlist",$userlist);
						$this->assign("guestlist",$guestlist);
						$this->assign("photolist",$photolist);
						$this->assign("manage",$manage);
						$this->display();
					}
				}else{
					$this->error("错误的操作");
				}
			}
			
			/**
			 * 活动留言
			 */
			public function activeguest(){
				$id = safeRequest($_GET["id"]);
				$record = D("active")->find("id=".$id,"title,privacy");
				if($record){
					if($record->privacy < 2 && !D("active_user")->find("activeid=".$id." and userid=".$this->userInfo->id,"id") ){
						$this->error("由于隐私设置，你不能浏览");	
					}else{
						isset($_GET["p"]) ? $p = $_GET["p"] : $p =1;
						$dao = new active_guestModel();
						import("ORG.Util.Page");
						$guestCount = $dao->count("activeid=".$id,"id");
						$listRows 	= 	C("publicListRows");
						$p 			= 	new Page($guestCount,$listRows);
						$p			->	setConfig('header' ,'个活动留言 '); 
						$guestlist	=	$dao->findAll("activeid=".$id,"*","regtime desc",$p->firstRow.','.$p->listRows);
						$page  		= 	$p->show();
						$this->assign("record",$record);
						$this->assign("guestlist",$guestlist);
						$this->assign("page",$page);
						$this->display();
					}
				}else{
					$this->error("错误的操作");
				}
				
			}
			
			/**
			 * 删除留言
			 */
			public function deleteguest(){
				$id = safeRequest($_GET["id"]);
				$dao = new active_guestModel();
				$record = $dao->find("id=".$id,"activeid,userid");
				if($record){
					if($record == $this->userInfo->id){//本人删除
						$dao->delete("id=".$id);
						isset($_GET["ajax"]) ? print "success" : $this->error("删除成功");
					}else{
						if(D("active_user")->find("activeid=".$record->activeid." and userid=".$this->userInfo->id." and usertype>1")){//管理员权限
							$dao->delete("id=".$id);
							isset($_GET["ajax"]) ? print "success" : $this->error("删除成功");
						}else{
							isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
						}
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
				}
			}
			
			/**
			 * 活动相册
			 */
			public function activephoto(){
				$id = safeRequest($_GET["id"]);
				$record = D("active")->find("id=".$id,"title,privacy");
				if($record){
					if($record->privacy < 2 && !D("active_user")->find("activeid=".$id." and userid=".$this->userInfo->id,"id") ){
						$this->error("由于隐私设置，你不能浏览");	
					}else{
						isset($_GET["p"]) ? $p = $_GET["p"] : $p =1;
						$dao = new active_photoModel();
						import("ORG.Util.Page");
						$photoCount = $dao->count("activeid=".$id,"id");
						$listRows 	= 	C("publicListRows");
						$p 			= 	new Page($photoCount,$listRows);
						$p			->	setConfig('header' ,'个活动留言 '); 
						$photolist	=	$dao->findAll("activeid=".$id,"*","regtime desc",$p->firstRow.','.$p->listRows);
						$page  		= 	$p->show();
						$this->assign("record",$record);
						$this->assign("photolist",$photolist);
						$this->assign("page",$page);
						$this->display();
					}
				}else{
					$this->error("错误的操作");
				}
			}
			
			/**
			 * 上传相片
			 */
			public function uploadphoto(){
				$id = safeRequest($_GET["id"]);
			}
			/**
			 * 保存上传
			 */
			public function saveupload(){
				
			}
			
			/**
			 * 编辑相片
			 */
			public function editphoto(){
				$id = safeRequest($_GET["id"]);
				$dao = new active_photoModel();
				$record = $dao->find("id=".$id." and userid=".$this->userInfo->id);
				if($record){
					$this->assign("record",$record);
					$this->display();
				}else{
					$this->error("错误的操作");
				}
			}
			
			/**
			 * 保存编辑
			 */
			public function updatephoto(){
				$id = safeRequest($_GET["id"]);
				$dao = new active_photoModel();
				$dao->find("id=".$id." and userid=".$this->userInfo->id);
				$dao->create();
				if($dao->save()){
					$this->success("编辑成功");
				}else{
					$this->error("编辑失败");
				}
			}
			
			/**
			 * 删除相片
			 */
			public function deletephoto(){
				$id = safeRequest($_GET["id"]);
				$dao = new active_photoModel();
				$record = $dao->find("id=".$this->userInfo->id,"activeid,userid");
				if($record){
					if($record->userid == $this->userInfo->id){//本人
						$dao->delete("id=".$id);
						@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/".$record->photoname);
						@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/small/".$record->photoname);
						@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/big/".$record->photoname);
						isset($_GET["ajax"]) ? print "success" : $this->success("删除相片成功");
					}else{
						if(D("active_user")->find("activeid=".$record->activeid." and userid=".$this->userInfo->id." and usertype>1")){//管理员
							$dao->delete("id=".$id);
							@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/".$record->photoname);
							@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/small/".$record->photoname);
							@unlink(dirname(__FILE__)."/../../../Public/Uploads/".$this->userInfo->id."/active/big/".$record->photoname);
							isset($_GET["ajax"]) ? print "success" : $this->success("删除相片成功");
						}else{
							isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
						}
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
				}
			}
			
			/**
			 * 活动报名
			 */
			public function joinactive(){
				$id = safeRequest($_GET["id"]);
				$dao = new activeModel();
				$record = $dao->find("id=".$id,"activename,strattime,endtime,privacy");
				if( 0== $record->privacy){//私密活动，不接受报名
					isset($_GET["ajax"]) ? print "error" : $this->error("操作失败");
				}
				$nowtime = time();
				if($nowtime < $record->starttime){
					isset($_GET["ajax"]) ? print "error" : $this->error("活动还未开始接受报名");
				}
				if($nowtime > $record->endtime){
					isset($_GET["ajax"]) ? print "error" : $this->error("活动报名已结束");
				}
				$userdao = D("active_user");
				if($userdao->find("activeid=".$id." and userid=".$this->userInfo->id)){
					isset($_GET["ajax"]) ? print "error" : $this->error("你已经加入了这个活动");
				}else{
					if(1==$record->privacy){ //需要管理员审核才能加入
						$requestdao = D("active_request");
						if($requestdao->find("activeid=".$id." and userid=".$this->userInfo->id,"id")){
							isset($_GET["ajax"]) ? print "error" : $this->error("你已经申请加入了这个活动");
						}else{
							$requestdao->activeid = $id;
							$requestdao->userid   = $this->userInfo->id;
							if($requestdao->add()){
								isset($_GET["ajax"]) ? print "success" : $this->success("你申请成功发出");
							}else{
								isset($_GET["ajax"]) ? print "error" : $this->error("操作失败");
							}
						}
					}elseif(2==$record->privacy){//公开活动，随时加入
						$userdao->activeid = $id;
						$userdao->userid = $this->userInfo->id;
						if($userdao->add()){
							$dao->setField("usernum","(usernum+1)","id=".$id);
							if($this->feedSetting){
								addFeed($this->userInfo->id,'active','加入了活动',$id);
							}
							isset($_GET["ajax"]) ? print "success" : $this->success("你成功加入这个活动");
						}else{
							isset($_GET["ajax"]) ? print "error" : $this->error("操作失败");
						}
					}
				}

			}
			
			/**
			 * 确定加入 (管理员操作)
			 */
			public function checkjoin(){
				$id = safeRequest($_GET["id"]);
				$u = safeRequest($_GET["u"]);
				$record = D("active_request")->find("activeid=".$id." and userid=".$u,"id");
				if($record){
					$dao = D("active_user");
					if($dao->find("activeid=".$id." and userid=".$this->userInfo->id." and usertype>1","id")){//是否管理员
						$dao->activeid = $id;
						$dao->userid = $u;
						if($dao->add()){
							D("active_request")->delete("id=".$record->id);
							D("active")->setField("usernum","(usernum+1)","id=".$id);
							if($this->feedSetting){
								addFeed($u,'active','加入了活动',$id);
							}
							addNotify($u,$this->userInfo->id,"activejoin",$id);//添加通知
							isset($_GET["ajax"]) ? print "success" : $this->error("成功加入这个活动");
						}else{
							isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
						}
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
				}
			}
			
			/**
			 * 接受邀请
			 */ 
			public function checkinvite(){
				$id = safeRequest($_GET["id"]);
				$u = $this->userInfo->id;
				$record = D("active_invite")->find("activeid=".$id." and userid=".$u,"id");
				if($record){
					$dao = D("active_user");
					$dao->activeid = $id;
					$dao->userid = $u;
					if($dao->add()){
						D("active_invite")->delete("id=".$record->id);
						D("active")->setField("usernum","(usernum+1)","id=".$id);
						if($this->feedSetting){
							addFeed($u,'active','加入了活动',$id);
						}
						isset($_GET["ajax"]) ? print "success" : $this->error("成功加入这个活动");
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("错误的操作");
				}
			}
			
			/**
			 * 活动成员列表
			 */
			public function activeuser(){
				$id = safeRequest($_GET["id"]);
				$record = D("active")->find("id=".$id,"activename,privacy");
				$dao = D("active_user");
				if(!$dao->find("activeid=".$id." and userid=".$this->userInfo->id,"id") && $record->privacy<2){
					$this->error("由于活动的隐私设置，你不能查看");
				}else{
					$userList = $dao->findAll("activeid=".$id);
					$this->assign("record",$record);
					$this->assign("uestList",$userList);
					$this->display();
				}
			}
			
		}
?>